package com.bm.jarquickstart.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 港某
 * @datetime 2021/4/25 17:22
 */
@Slf4j
public class FileUtil {

    public static final Map<String, File> FILE_MAP = new ConcurrentHashMap<>();

    private static final String OS = System.getProperty("os.name").toLowerCase();

    public static Properties getProp() {
        Properties properties = new Properties();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("setting.properties");
            properties.load(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return properties;
    }

    public static void getFileList() {
        Properties prop = getProp();
        String jarPath = prop.getProperty("jarPath");
        if (StringUtils.isBlank(jarPath)) {
            return;
        }
        FILE_MAP.clear();
        File file = new File(jarPath);
        searchFile(file.listFiles());
        log.info("扫描结束，发现文件总数：【{}】", FILE_MAP.size());
    }

    private static void searchFile(File[] files) {
        if (files == null || files.length <= 0) {
            return;
        }
        for (File f : files) {
            if (f.isDirectory()) {
                searchFile(f.listFiles());
            } else {
                FILE_MAP.put(UUID.randomUUID().toString(), f);
            }
        }
    }

    public static void jarStart(File file) {
        System.out.println(System.getenv());
        log.info("当前操作系统为：{}", OS);
        if (OS.contains("windows")) {

        } else {

        }

    }

    public static void main(String[] args) {
        File file = new File("D:\\Users\\20064\\drmc\\drmc-service\\drmc-service-operationlog\\target\\drmc-service-operationlog.jar");
        jarStart(file);
    }

}
